﻿using NL.ADA.ConfigService.DAL.EntityClasses;
using NL.ADA.Common;
using NL.ADA.ConfigService.DAL.HelperClasses;
using NL.ADA.ConfigAdmin;

namespace NL.ADA.AdminService
{
    // NOTE: If you change the class name "AdminService" here, you must also update the reference to "AdminService" in Web.config and in the associated .svc file.
    public class AdminService : IAdminService
    {
        public AdminService()
        {
        }

        public CustomConfigList GetAllConfigs(int appId, int modeId)
        {
            var mgr = new ConfigManager();
            return mgr.GetAllConfigs(appId, modeId);
        }

        public CustomConfig GetConfigById(int id)
        {
            var result = new CustomConfig();
            var mgr = new ConfigManager();
            ConfigEntity entity = mgr.GetById(id);
            result.ConfigId = entity.Id;
            result.ConfigTypeId = entity.ConfigType;
            result.ApplicationId = entity.ApplicationId;
            result.Key = entity.Key;
            result.Value = entity.Value;
            return result;
        }

        public int UpdateConfig(CustomConfig config)
        {
            var mgr = new ConfigManager();
            var entity = new ConfigEntity();
            entity.Id = config.ConfigId;
            entity.ApplicationId = config.ApplicationId;
            entity.ModeId = config.ModeId;
            entity.Key = config.Key;
            entity.Value = config.Value;
            entity.ConfigType = config.ConfigTypeId;
            return mgr.Update(entity);
        }

        public EntityCollection GetApplications()
        {
            var mgr = new ApplicationManager();
            return mgr.Get();
        }

        public int InsertConfig(CustomConfig config)
        {
            var mgr = new ConfigManager();
            var entity = new ConfigEntity();
            entity.Id = config.ConfigId;
            entity.ApplicationId = config.ApplicationId;
            entity.ModeId = config.ModeId;
            entity.Key = config.Key;
            entity.Value = config.Value;
            entity.ConfigType = config.ConfigTypeId;
            return mgr.Insert(entity);

        }
        public bool Deleteconfig(int configId)
        {
            var mgr = new ConfigManager();
            return mgr.Delete(configId);
        }


        public EntityCollection GetAllModes()
        {
            var mgr = new ModeManager();
            return mgr.Get();
        }


        #region IAdminService Members


        public EntityCollection GetAllConfigTypes()
        {
            var mgr = new ConfigTypeManager();
            return mgr.Get();
        }

        #endregion

        #region IAdminService Members


        public CustomConfig GetConfigByKey(string key,int appId,int modeId)
        {
            var result = new CustomConfig();
            var mgr = new ConfigManager();
            var entity = mgr.GetConfigByKey(key, appId, modeId);
            result.ConfigId = entity.Id;
            result.Key = entity.Key;
            result.Value = entity.Value;
            result.ModeId = entity.ModeId;
            result.ApplicationId = entity.ApplicationId;
            result.ConfigTypeId = entity.ConfigType;
            return result;
        }



        public int InsertApplication(ApplicationEntity entity)
        {
            var mgr = new ApplicationManager();
            return mgr.Insert(entity);
        }

        #endregion

        #region IAdminService Members


        public ApplicationEntity SelectApplicationById(int id)
        {
            var mgr = new ApplicationManager();
            return mgr.GetById(id);
        }

        #endregion
    }
}
